package com.hzjy.common.utils;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 将多张图片合并转为PDF；需要用到iTextpdf包，
 *
 * @author 浴缸
 */
public class Convert2Pdf {
    /**
     * @param imageFolderPath 图片文件夹地址
     * @param pdfPath         PDF文件保存地址
     */
    public static void toPdf(String imageFolderPath, String pdfPath) {
        try {
            // 图片文件夹地址
            // String imageFolderPath = "D:/Demo/ceshi/";
            // 图片地址
            String imagePath = null;
            // PDF文件保存地址
            // String pdfPath = "D:/Demo/ceshi/hebing.pdf";
            // 输入流
            FileOutputStream fos = new FileOutputStream(pdfPath);
            // 创建文档
            Document doc = new Document(null, 0, 0, 0, 0);
            //doc.open();
            // 写入PDF文档
            PdfWriter.getInstance(doc, fos);
            // 读取图片流
            BufferedImage img = null;
            // 实例化图片
            Image image = null;
            // 获取图片文件夹对象
            File file = new File(imageFolderPath);
            File[] files = file.listFiles();
            // 循环获取图片文件夹内的图片
            for (File file1 : files) {
                if (file1.getName().endsWith(".png")
                        || file1.getName().endsWith(".jpg")
                        || file1.getName().endsWith(".gif")
                        || file1.getName().endsWith(".jpeg")
                        || file1.getName().endsWith(".tif")) {
                    // System.out.println(file1.getName());
                    imagePath = imageFolderPath + file1.getName();
                    System.out.println(file1.getName());
                    // 读取图片流
                    img = ImageIO.read(new File(imagePath));
                    // 根据图片大小设置文档大小
                    doc.setPageSize(new Rectangle(img.getWidth(), img
                            .getHeight()));
                    // 实例化图片
                    image = Image.getInstance(imagePath);
                    // 添加图片到文档
                    doc.open();
                    doc.add(image);
                }
            }
            // 关闭文档
            doc.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BadElementException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public static void toPdf(List<String> imgList, String pdfPath) {
        try {
            // 图片文件夹地址
            // String imageFolderPath = "D:/Demo/ceshi/";
            // 图片地址
//            String imagePath = null;
            // PDF文件保存地址
            // String pdfPath = "D:/Demo/ceshi/hebing.pdf";
            // 输入流
            FileOutputStream fos = new FileOutputStream(pdfPath);
            // 创建文档
            Document doc = new Document(null, 0, 0, 0, 0);
            //doc.open();
            // 写入PDF文档
            PdfWriter.getInstance(doc, fos);
            // 读取图片流
            BufferedImage img = null;
            // 实例化图片
            Image image = null;
            for (String imgPh : imgList) {
//                imgPh = "file:///" + imgPh;
                File file1 = new File(imgPh);
                if (file1.getName().endsWith(".png")
                        || file1.getName().endsWith(".jpg")
                        || file1.getName().endsWith(".gif")
                        || file1.getName().endsWith(".jpeg")
                        || file1.getName().endsWith(".tif")) {
                    // System.out.println(file1.getName());
//                    imagePath = imageFolderPath + file1.getName();
//                    System.out.println("===========imgPh1==========" + imgPh);
//                        imgPh.replaceAll("\"","");
//                    System.out.println("===========imgPh2==========" + imgPh);
                    System.out.println(imgPh);
                    InputStream murl = new URL(imgPh).openStream();
                    img = ImageIO.read(murl);

                    // 读取图片流
//                    img = ImageIO.read(file1);
                    // 根据图片大小设置文档大小
                    doc.setPageSize(new Rectangle(img.getWidth(), img
                            .getHeight()));
                    // 实例化图片
                    image = Image.getInstance(imgPh);
                    // 添加图片到文档
                    doc.open();
                    doc.add(image);
                }
            }
            doc.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BadElementException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    /**
     * 递归删除目录下的所有文件及子目录下所有文件
     *
     * @param dir 将要删除的文件目录
     * @return boolean Returns "true" if all deletions were successful.
     * If a deletion fails, the method stops attempting to
     * delete and returns "false".
     */
    public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            //递归删除目录中的子目录下
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // 目录此时为空，可以删除
        return dir.delete();
    }

    public static void main(String[] args) {
        long time1 = System.currentTimeMillis();
        List<String> list = new ArrayList<>();
        list.add(0, "http:\\/\\/119.97.184.132:8080\\/profile\\/upload\\/2020\\/08\\/31\\/c0806e5e-5d91-43b1-859b-c6c545a3f7c1.jpeg");
        list.add(1, "http:\\/\\/119.97.184.132:8080\\/profile\\/upload\\/2020\\/08\\/31\\/e2b819d4-e5b1-41ed-b1ac-a185501c14b4.jpeg");
        list.add(2, "http:\\/\\/119.97.184.132:8080\\/profile\\/upload\\/2020\\/08\\/31\\/f52ccb47-c155-4a64-8885-f031cc6812a1.jpeg");
        list.add(3, "http:\\/\\/119.97.184.132:8080\\/profile\\/upload\\/2020\\/08\\/31\\/c229dc05-81dd-429a-9b51-cb5cc85bc17f.jpeg");
//        toPdf("http://119.97.184.132:8080/profile/upload/2020/08/29/07bf2a36-54ff-4ef4-9a3c-14a46a77a3f3.png", "D:/test/test1.pdf");
//        toPdf("http://119.97.184.132:8080/profile/upload/2020/08/29/1e6aafe6-ac36-4a97-9e0c-3108439e7449.png", "D:/test/test1.pdf");
//        toPdf("http://119.97.184.132:8080/profile/upload/2020/08/29/1e736c7c-fd4c-4c66-9385-037c1effc456.png", "D:/test/test1.pdf");
        toPdf(list, "D:/test/222/test22.pdf");
        long time2 = System.currentTimeMillis();
        int time = (int) ((time2 - time1) / 1000);
        System.out.println("执行了：" + time + "秒！");
    }

}
